/*
+----------------------------------------------------------------------+
| MD                                                              |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license,    |
| that is bundled with this package in the file LICENSE, and is        |
| available through the world-wide-web at the following url:           |
| http://www.apache.org/licenses/LICENSE-2.0.html                      |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to       |
| license@php.net so we can mail you a copy immediately.               |
+----------------------------------------------------------------------+
| Author: vb2005xu  <vb2005xu@qq.com>                                  |
+----------------------------------------------------------------------+
*/

#include "php_md.h"

//声明当前扩展的全局变量
ZEND_DECLARE_MODULE_GLOBALS(md);

//初始化全局变量时执行
static PHP_GINIT_FUNCTION(md);

//释放全局变量时执行
static PHP_GSHUTDOWN_FUNCTION(md);

#ifdef COMPILE_DL_MD
ZEND_GET_MODULE(md)
#endif


const zend_function_entry md_functions[] =
{
	PHP_FE(md_confirm_compiled, NULL)
    PHP_FE(md_get_version, NULL)
    PHP_FE(md_get_author,  NULL)    
    {
        NULL, NULL, NULL
    }
};

#if ZEND_MODULE_API_NO >= 20050922
zend_module_dep md_deps[] =
{
    {
        NULL, NULL, NULL
    }
};
#endif

// 定义 ini 选项
PHP_INI_BEGIN()

// retype 在 ZEND_BEGIN_MODULE_GLOBALS 中声明
STD_PHP_INI_BOOLEAN("md.retype", "0", PHP_INI_SYSTEM, OnUpdateBool, retype, zend_md_globals, md_globals)

PHP_INI_END()

// === 生命周期函数定义

// 初始化module时运行
PHP_MINIT_FUNCTION(md)
{
	zend_class_entry md;

	REGISTER_INI_ENTRIES();

	// 定义字符串常量
	REGISTER_STRINGL_CONSTANT("MD_VERSION",   MD_VERSION,   sizeof(MD_VERSION) - 1,  CONST_PERSISTENT | CONST_CS);
    REGISTER_STRINGL_CONSTANT("MD_AUTHOR",    MD_AUTHOR,    sizeof(MD_AUTHOR) - 1,   CONST_PERSISTENT | CONST_CS);

    return SUCCESS;
}

//当module被卸载时运行
PHP_MSHUTDOWN_FUNCTION(md)
{
	UNREGISTER_INI_ENTRIES();

    return SUCCESS;
}

//当一个REQUEST请求初始化时运行
PHP_RINIT_FUNCTION(md)
{

    return SUCCESS;
}

//当一个REQUEST请求结束时运行
PHP_RSHUTDOWN_FUNCTION(md)
{
	return SUCCESS;
}

// 设置phpinfo中此模块展示的信息
PHP_MINFO_FUNCTION(md)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "md support", "Enabled");
    php_info_print_table_row(2, "md version", MD_VERSION);
    php_info_print_table_row(2, "md author", MD_AUTHOR);
    php_info_print_table_row(2, "md supports", MD_SUPPORTS);
    php_info_print_table_end();

    DISPLAY_INI_ENTRIES();
}

//初始化全局变量时执行
static PHP_GINIT_FUNCTION(md)
{
    memset(md_globals, 0, sizeof(zend_md_globals));
}

//释放全局变量时执行
static PHP_GSHUTDOWN_FUNCTION(md)
{

}

// 全局实现函数

// 定义模块对象
zend_module_entry md_module_entry =
{
#if ZEND_MODULE_API_NO >= 20050922
    STANDARD_MODULE_HEADER_EX, NULL,
    md_deps,
#else
    STANDARD_MODULE_HEADER,
#endif
    MD_RES_NAME,
    md_functions,
    PHP_MINIT(md),
    PHP_MSHUTDOWN(md),
    PHP_RINIT(md),
    PHP_RSHUTDOWN(md),
    PHP_MINFO(md),
    MD_VERSION,
    PHP_MODULE_GLOBALS(md),
    PHP_GINIT(md),
    PHP_GSHUTDOWN(md),
    NULL,
    STANDARD_MODULE_PROPERTIES_EX
};

// 定义字符串格式化函数
PHPAPI int md_printf(const char *fmt, ...)
{
    char *buf;
    size_t len, written;
    va_list argv;

    va_start(argv, fmt);
    len = vspprintf(&buf, 0, fmt, argv);
    va_end(argv);

    written = php_output_write(buf, len);
    efree(buf);
    return written;
}

PHPAPI int md_print(const char *str)
{
    return php_output_write(str, strlen(str));
}

PHPAPI int md_println(const char *str)
{
    return md_printf("%s\n", str);
}

PHPAPI int md_nullLines(int num_lines)
{
    int i;
    for (i=0; i<num_lines; i++) {
        md_print("\n");
    }
}

PHP_FUNCTION(md_confirm_compiled)
{
    char *arg = NULL;
    size_t arg_len, len;
    zend_string *strg;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
        return;
    }

    strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "md", arg);

    RETURN_STR(strg);
}

PHP_FUNCTION(md_get_markdownhelp)
{
    RETURN_STRING('https://guides.github.com/features/mastering-markdown/');
}

PHP_FUNCTION(md_get_version)
{
    MD_RETURN_STRINGL(MD_VERSION, strlen(MD_VERSION));
}

PHP_FUNCTION(md_get_author)
{
    MD_RETURN_STRINGL(MD_AUTHOR, strlen(MD_AUTHOR));
}

// 大标题
PHP_FUNCTION(md_title)
{
    zval *_base_path;
    int argc = ZEND_NUM_ARGS();

    if (zend_parse_parameters(argc TSRMLS_CC, "z", &_base_path) == FAILURE)
    {
        return;
    }

    
}
// 副标题
// PHP_FUNCTION(md_subtitle);
// 章标题
PHP_FUNCTION(md_chapter_title)
{
    
}
// 节标题 1.1
PHP_FUNCTION(md_section_title)
{
    
}
// 条标题 1.1.1
PHP_FUNCTION(md_section_subtitle)
{
    
}
// 目录区
PHP_FUNCTION(md_catalog_start)
{
    
}
PHP_FUNCTION(md_catalog_end)
{
    
}
// 分隔线
PHP_FUNCTION(md_ruler)
{
    
}
// 段落
PHP_FUNCTION(md_paragraph)
{
    
}
// 引用
PHP_FUNCTION(md_blockquote)
{
    
}
// 范例
PHP_FUNCTION(md_code_start)
{
    
}
PHP_FUNCTION(md_code_end)
{
    
}
// 插图
PHP_FUNCTION(md_img)
{
    
}
// 链接
PHP_FUNCTION(md_link)
{
    
}
